#include "tabledialog.h"
#include <QMouseEvent>
#include <QPainter>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>

static const int CELL_SIZE = 20;
static const int MAX_CELLS = 10;

TableSelectionWidget::TableSelectionWidget(QWidget *parent)
    : QWidget(parent), m_rows(0), m_columns(0), m_hoveredRows(0), m_hoveredColumns(0)
{
    setMouseTracking(true);
    const int spacing = 3;
    // 调整大小以适应小方块布局
    setMinimumSize(MAX_CELLS * (CELL_SIZE + spacing), MAX_CELLS * (CELL_SIZE + spacing));
    setMaximumSize(MAX_CELLS * (CELL_SIZE + spacing), MAX_CELLS * (CELL_SIZE + spacing));
}

void TableSelectionWidget::updateSelection(const QPoint &pos)
{
    const int spacing = 3;
    int newHoveredColumns = qBound(1, pos.x() / (CELL_SIZE + spacing) + 1, MAX_CELLS);
    int newHoveredRows = qBound(1, pos.y() / (CELL_SIZE + spacing) + 1, MAX_CELLS);

    if (newHoveredRows != m_hoveredRows || newHoveredColumns != m_hoveredColumns) {
        m_hoveredRows = newHoveredRows;
        m_hoveredColumns = newHoveredColumns;
        emit hoverSizeChanged(m_hoveredRows, m_hoveredColumns);
        update();
    }
}

void TableSelectionWidget::mouseMoveEvent(QMouseEvent *event)
{
    updateSelection(event->pos());
}

void TableSelectionWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_rows = m_hoveredRows;
        m_columns = m_hoveredColumns;
        emit sizeSelected(m_rows, m_columns);
        emit selectionConfirmed(m_rows, m_columns);
    }
}

void TableSelectionWidget::leaveEvent(QEvent *event)
{
    // 鼠标离开时重置悬停状态
    m_hoveredRows = 0;
    m_hoveredColumns = 0;
    emit hoverSizeChanged(0, 0);
    update();
    QWidget::leaveEvent(event);
}

void TableSelectionWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(rect(), Qt::white);

    const int spacing = 3;

    // 绘制表格选择区域 - 使用小方块而不是网格线
    for (int row = 0; row < MAX_CELLS; ++row) {
        for (int col = 0; col < MAX_CELLS; ++col) {
            int x = col * (CELL_SIZE + spacing);
            int y = row * (CELL_SIZE + spacing);

            // 绘制小方块
            if (row < m_hoveredRows && col < m_hoveredColumns) {
                // 被选中的单元格
                painter.fillRect(x, y, CELL_SIZE, CELL_SIZE, QColor(0, 94, 134));
            } else {
                // 未选中的单元格
                painter.fillRect(x, y, CELL_SIZE, CELL_SIZE, Qt::white);
                painter.setPen(Qt::black);  // 使用黑色边框
                painter.drawRect(x, y, CELL_SIZE, CELL_SIZE);
            }
        }
    }
}

TableDialog::TableDialog(QWidget *parent)
    : QDialog(parent)
{
    // 移除标题栏
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
    setModal(true);

    // 创建尺寸标签
    sizeLabel = new QLabel("0 行 × 0 列, 表格");
    sizeLabel->setAlignment(Qt::AlignLeft);

    // 创建表格选择器
    selectionWidget = new TableSelectionWidget(this);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(10, 10, 10, 10);
    mainLayout->setSpacing(5); // 减小间距
    mainLayout->addWidget(sizeLabel);
    mainLayout->addWidget(selectionWidget, 0, Qt::AlignCenter);

    setLayout(mainLayout);

    // 连接信号和槽
    connect(selectionWidget, &TableSelectionWidget::sizeSelected, this, &TableDialog::updateSizeLabel);
    connect(selectionWidget, &TableSelectionWidget::selectionConfirmed, this, &TableDialog::accept);
    connect(selectionWidget, &TableSelectionWidget::hoverSizeChanged, this, &TableDialog::updateHoverSizeLabel);

    // 初始化大小
    sizeHint();
    setMinimumSize(250, 280);
    updateGeometry();
}

void TableDialog::updateSizeLabel(int rows, int columns)
{
    m_rows = rows;
    m_columns = columns;
    updateHoverSizeLabel(rows, columns);
}

void TableDialog::updateHoverSizeLabel(int rows, int columns)
{
    // 当没有悬停在表格上时，显示默认消息或之前选中的大小
    if (rows == 0 || columns == 0) {
        if (m_rows > 0 && m_columns > 0) {
            sizeLabel->setText(QString("%1 行 × %2 列, 表格").arg(m_rows).arg(m_columns));
        } else {
            sizeLabel->setText("请选择表格大小");
        }
    } else {
        sizeLabel->setText(QString("%1 行 × %2 列, 表格").arg(rows).arg(columns));
    }
}

int TableDialog::rows() const
{
    return m_rows;
}

int TableDialog::columns() const
{
    return m_columns;
}
